package com.example.mydefinedview1

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import androidx.annotation.LayoutRes
import androidx.annotation.StringRes
import androidx.fragment.app.Fragment
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2
import com.example.mydefinedview1.base.BaseActivity
import com.example.mydefinedview1.databinding.ActivityMainBinding
import com.example.mydefinedview1.ui.SimpleCircleActivity
import com.google.android.material.tabs.TabLayout

class MainActivity : BaseActivity<ActivityMainBinding>(){
    private val pageModels = arrayListOf<PageModel>()
    fun init() {
        pageModels.add(PageModel(R.layout.sample_color,R.string.title_draw_color,R.layout.color_view))
        pageModels.add(PageModel(R.layout.sample_circle,R.string.title_draw_circle,R.layout.circle_view))
        pageModels.add(PageModel(R.layout.sample_rect,R.string.title_draw_rect,R.layout.rect_view))
        pageModels.add(PageModel(R.layout.sample_point,R.string.title_draw_point,R.layout.point_view))
        pageModels.add(PageModel(R.layout.sample_oval,R.string.title_draw_oval,R.layout.oval_view))
        pageModels.add(PageModel(R.layout.sample_line,R.string.title_draw_line,R.layout.line_view))
        pageModels.add(PageModel(R.layout.sample_round_rect,R.string.title_draw_round_rect,R.layout.round_rect_view))
        pageModels.add(PageModel(R.layout.sample_arc,R.string.title_draw_arc,R.layout.arc_view))
        pageModels.add(PageModel(R.layout.sample_path,R.string.title_draw_path,R.layout.path_view))
        pageModels.add(PageModel(R.layout.sample_histogram,R.string.title_draw_histogram,R.layout.histogram_view))
        pageModels.add(PageModel(R.layout.sample_pie_chart,R.string.title_draw_pie_chart,R.layout.pie_chart_view))
    }

    override fun ActivityMainBinding.initBinding() {
        init()
        for (i in pageModels.indices){
            tabLayout.addTab(tabLayout.newTab().setText(pageModels[i].titleRes))
        }
        mBinding.paper.adapter = object : FragmentStateAdapter(this@MainActivity) {
            override fun getItemCount(): Int = pageModels.size

            override fun createFragment(position: Int): Fragment {
                val pageModel = pageModels[position]
                return PageFragment.newInstance(pageModel.sampleLayoutRes,pageModel.practiceLayoutRes)
            }
        }
        mBinding.tabLayout.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener{
            override fun onTabSelected(tab: TabLayout.Tab?) {
                paper.currentItem = tab?.position!!
            }

            override fun onTabUnselected(tab: TabLayout.Tab?) {

            }

            override fun onTabReselected(tab: TabLayout.Tab?) {

            }
        })

        paper.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback(){
            override fun onPageSelected(position: Int) {
                tabLayout.setScrollPosition(position,0F,false)
            }
        })
    }

}
data class PageModel(
    var sampleLayoutRes:Int,
    var titleRes:Int,
    var practiceLayoutRes:Int
)